package com.sgzn.sgpay.util;

import com.google.common.base.Strings;

import java.util.Date;

/**
 * 序列号生成工具类
 * @author zhongzhongyong
 * @version Id: SeqUtil.java, v 0.1 2018/4/8 13:43 zhongzhongyong Exp $$
 */
public class SeqUtil {

    /**
     * 目前统一为32位序列号
     * @param flag 2位标记
     * @return
     */
    public static String getCommonSeq(String flag){
        int randomNum = (int)((Math.random()*9+1)*10000000);//8位随机数
        String orignalString = String.valueOf(randomNum);
        String dateStr = DateUtil.formatDate(new Date(),DateUtil.TIME_FORMAT);//14位
        return getFormatSeq(dateStr, flag, orignalString,  '0', 16);
    }

    /**
     * 目前统一为16位序列号
     * @param flag 2位标记
     * @return
     */
    public static String getShortSeq(String flag){
        int randomNum = (int)((Math.random()*9+1)*10);//4位随机数
        String orignalString = String.valueOf(randomNum);
        String dateStr = DateUtil.formatDate(new Date(),DateUtil.TIME_FORMAT).substring(2, 14);;//12位
        return getFormatSeq(dateStr, flag, orignalString,  '0', 2);
    }

    /**
     * 目前统一为8位序列号
     * @return
     */
    public static String getSmallSeq(){
        String orignalString = RandomUtil.generateRandomNumber(2);
        String dateStr = DateUtil.formatDate(new Date(),DateUtil.TIME_FORMAT).substring(8, 14);;//12位
        return dateStr+orignalString;
    }

    public static String getFormatSeq(String prefix,String flag ,String orignalString, char fillchar, int lenth){
        return prefix + flag + Strings.padStart(orignalString, lenth, fillchar);
    }

}
